package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.pojo.entity.Admin;
import com.example.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 管理员 controller 层
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
        this.adminService.add(admin);
        return Result.success();
    }

    @GetMapping("/pageQuery")
    public Result pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Admin admin
    ) {
        PageResult<Admin> pageResult = this.adminService.pageQuery(pageNum, pageSize, admin);
        return Result.success(pageResult);
    }

    @GetMapping("/query")
    public Result queryById(@RequestParam Integer id) {
        Admin admin = this.adminService.lambdaQuery()
                .eq(Admin::getId, id)
                .one();
        admin.setPassword(null);
        return Result.success(admin);
    }

    @GetMapping("/query/all")
    public Result queryAll() {
        List<Admin> adminList = this.adminService.lambdaQuery()
                .list();
        for (Admin admin : adminList) {
            admin.setPassword(null);
        }
        return Result.success(adminList);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        this.adminService.update(admin);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        this.adminService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        this.adminService.deleteBatch(ids);
        return Result.success();
    }
}
